package com.itheima.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.entity.SmallPageBean;
import com.itheima.exception.CheckItemDeleteFailException;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Lin
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    //注入checkItemService
    //查找服务
    @Reference
    private CheckGroupService checkGroupService;

    //添加数据 -- add
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //分页查询 -- findPage -- 调用service层的pageQuery(根据条件查询) -- 调用dao层方法selectByCondition(queryString) 动态执行sql
    //条件不为空 就执行模糊查询（因为有可能输关键字）
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //调用方法
        return checkGroupService.pageQuery(queryPageBean);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        //调用方法
        try {
            checkGroupService.edit(checkGroup,checkItemIds);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(String checkGroupId){
        List<Integer> checkItemIds = null;
        try {
            checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(checkGroupId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
    }

    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody SmallPageBean smallPageBean){
        //调用方法
        return checkGroupService.findAll(smallPageBean);
    }

}
